home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / dev / debug / Blowup.lha / source / tools.c < prev    next >
C/C++ Source or Header  |  1998-04-18  |  3KB  |  171 lines

  1. /*
  2.  * $Id: tools.c 1.3 1998/04/18 15:45:46 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Blowup -- Catches and displays task errors
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. VOID
  19. StrcpyN(LONG MaxLen,STRPTR To,const STRPTR From)
  20. {
  21.     ASSERT(To != NULL && From != NULL);
  22.  
  23.     /* copy a string, but only up to MaxLen characters */
  24.  
  25.     if(MaxLen > 0)
  26.     {
  27.         LONG Len = strlen(From);
  28.  
  29.         if(Len >= MaxLen)
  30.             Len = MaxLen - 1;
  31.  
  32.         strncpy(To,From,Len);
  33.         To[Len] = '\0';
  34.     }
  35. }
  36.  
  37. /******************************************************************************/
  38.  
  39. struct FormatContext
  40. {
  41.     STRPTR    Index;
  42.     LONG    Size;
  43.     BOOL    Overflow;
  44. };
  45.  
  46. STATIC VOID ASM
  47. StuffChar(
  48.     REG(a3)    struct FormatContext *    Context,
  49.     REG(d0) UBYTE                    Char)
  50. {
  51.     /* Is there still room? */
  52.     if(Context->Size > 0)
  53.     {
  54.         (*Context->Index) = Char;
  55.  
  56.         Context->Index++;
  57.         Context->Size--;
  58.  
  59.         /* Is there only a single character left? */
  60.         if(Context->Size == 1)
  61.         {
  62.             /* Provide null-termination. */
  63.             (*Context->Index) = '\0';
  64.  
  65.             /* Don't store any further characters. */
  66.             Context->Size = 0;
  67.         }
  68.     }
  69.     else
  70.     {
  71.         Context->Overflow = TRUE;
  72.     }
  73. }
  74.  
  75. BOOL
  76. VSPrintfN(
  77.     LONG            MaxLen,
  78.     STRPTR            Buffer,
  79.     const STRPTR    FormatString,
  80.     const va_list    VarArgs)
  81. {
  82.     BOOL result = FAILURE;
  83.  
  84.     /* format a text, but place only up to MaxLen
  85.      * characters in the output buffer (including
  86.      * the terminating NUL)
  87.      */
  88.  
  89.     ASSERT(Buffer != NULL && FormatString != NULL);
  90.  
  91.     if(MaxLen > 1)
  92.     {
  93.         struct FormatContext Context;
  94.  
  95.         Context.Index        = Buffer;
  96.         Context.Size        = MaxLen;
  97.         Context.Overflow    = FALSE;
  98.  
  99.         RawDoFmt(FormatString,(APTR)VarArgs,(VOID (*)())StuffChar,(APTR)&Context);
  100.  
  101.         if(NO Context.Overflow)
  102.             result = SUCCESS;
  103.     }
  104.  
  105.     return(result);
  106. }
  107.  
  108. BOOL
  109. SPrintfN(
  110.     LONG            MaxLen,
  111.     STRPTR            Buffer,
  112.     const STRPTR    FormatString,
  113.                     ...)
  114. {
  115.     va_list VarArgs;
  116.     BOOL result;
  117.  
  118.     /* format a text, varargs version */
  119.  
  120.     ASSERT(Buffer != NULL && FormatString != NULL);
  121.  
  122.     va_start(VarArgs,FormatString);
  123.     result = VSPrintfN(MaxLen,Buffer,FormatString,VarArgs);
  124.     va_end(VarArgs);
  125.  
  126.     return(result);
  127. }
  128.  
  129. /******************************************************************************/
  130.  
  131. STATIC VOID
  132. TimeValToDateStamp(
  133.     const struct timeval *    tv,
  134.     struct DateStamp *        ds)
  135. {
  136.     /* convert a timeval to a DateStamp */
  137.  
  138.     ds->ds_Days        = tv->tv_secs / (24 * 60 * 60);
  139.     ds->ds_Minute    = (tv->tv_secs % (24 * 60 * 60)) / 60;
  140.     ds->ds_Tick        = (tv->tv_secs % 60) * TICKS_PER_SECOND + (tv->tv_micro * TICKS_PER_SECOND) / 1000000;
  141. }
  142.  
  143. /******************************************************************************/
  144.  
  145. VOID
  146. ConvertTimeAndDate(
  147.     const struct timeval *        tv,
  148.     STRPTR                        dateTimeBuffer)
  149. {
  150.     UBYTE dateBuffer[LEN_DATSTRING];
  151.     UBYTE timeBuffer[LEN_DATSTRING];
  152.     struct DateTime dat;
  153.  
  154.     /* convert a timeval into a human-readable date and time text */
  155.  
  156.     ASSERT(tv != NULL);
  157.  
  158.     memset(&dat,0,sizeof(dat));
  159.  
  160.     TimeValToDateStamp(tv,&dat.dat_Stamp);
  161.  
  162.     dat.dat_Format    = FORMAT_DOS;
  163.     dat.dat_StrDate    = dateBuffer;
  164.     dat.dat_StrTime    = timeBuffer;
  165.  
  166.     DateToStr(&dat);
  167.  
  168.     strcpy(dateTimeBuffer,dateBuffer);
  169.     strcat(dateTimeBuffer,timeBuffer);
  170. }
  171.